package ObjGitModel

import (
	"arabApi/app/api/models"
	"arabApi/app/api/requests/ObjGitRequest"
	"arabApi/pkg/database"
	"arabApi/pkg/helpers"
)

// TpGitBranch  git 分支表
type TpGitBranch struct {
	models.BaseModel
	ObId   int64  `gorm:"column:ob_id" db:"ob_id" json:"ob_id" form:"ob_id"`     //  项目id
	Desc   string `gorm:"column:desc" db:"desc" json:"desc" form:"desc"`         //  描述
	Branch string `gorm:"column:branch" db:"branch" json:"branch" form:"branch"` //  分支
	Step   int64  `gorm:"column:step" db:"step" json:"step" form:"step"`         //  第几步了 1 初始 2 测试环境 3 开发环境 4 正式环境
	Msg    string `gorm:"column:msg" db:"msg" json:"msg" form:"msg"`
	models.CommonTimestampsField
}

func (TpGitBranch) TableName() string {
	return "tp_git_branch"
}

func BranchList(r ObjGitRequest.GitBranch) ([]TpGitBranch, int64) {
	var count int64
	var list []TpGitBranch
	database.DB.Model(TpGitBranch{}).Where("ob_id = ?", r.ID).
		Count(&count).Limit(r.PageSize).Offset((r.Page - 1) * r.PageSize).Order("id desc").Find(&list)
	return list, count
}

func BranchMsgByid(r int64) TpGitBranch {
	var msg TpGitBranch

	database.DB.Where("id = ?", r).Find(&msg)
	return msg
}

func BranchMsgByStep(r int64) []TpGitBranch {
	var msg []TpGitBranch

	database.DB.Where("step = 4").Where("id = ?", r).Find(&msg)
	return msg
}

func BranchAdd(r ObjGitRequest.GitBranch) (uint64, error) {
	s := &TpGitBranch{}

	if err := helpers.Copy(r, s); err != nil {
		return 0, err
	}
	res := database.DB.Model(s).Create(s)
	return s.ID, res.Error
}

func BranchEdit(r ObjGitRequest.GitBranch) error {
	s := &TpGitBranch{}

	if err := helpers.Copy(r, s); err != nil {
		return err
	}
	res := database.DB.Model(s).Where("id = ?", r.ID).Updates(s)
	return res.Error
}

func EditBranchStep(id, step int) error {
	s := &TpGitBranch{}

	res := database.DB.Model(s).Where("id = ?", id).Update("step", step)
	return res.Error
}

func EditBranchMsg(id int, msg string) error {
	s := &TpGitBranch{}

	res := database.DB.Model(s).Where("id = ?", id).Update("msg", msg)
	return res.Error
}
